package o;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.impl.DownloadService;
import com.huawei.appgallery.downloadengine.impl.DownloadService.c;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

public class bnr {
    private static Object ʽ = new byte[0];
    private static volatile DownloadService ˊ;
    private static b ˋ = null;
    private static List<String> ˎ = new LinkedList();
    private static Object ˏ = new byte[0];
    private static List<Long> ॱ = new LinkedList();

    static class b implements ServiceConnection {
        b() {
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            int size;
            Logger.ˎ("HiAppDownload", "DownloadService onServiceConnected");
            bnr.ˊ = ((c) iBinder).ॱ();
            synchronized (bnr.ˏ) {
                for (Long longValue : bnr.ॱ) {
                    bnr.ˊ.doDownload(longValue.longValue());
                }
                size = bnr.ˎ.size();
                bnr.ˎ.clear();
            }
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            linkedHashMap.put("errorMsg", String.valueOf(size));
            linkedHashMap.put("errorCode", String.valueOf(0));
            linkedHashMap.put("option", String.valueOf(2));
            cdl.ˎ("038", linkedHashMap);
        }

        public void onServiceDisconnected(ComponentName componentName) {
            Logger.ˎ("HiAppDownload", "DownloadService onServiceDisconnected");
            bnr.ˊ = null;
            synchronized (bnr.ʽ) {
                bnr.ˋ = null;
            }
        }
    }

    public static void ˋ(SessionDownloadTask sessionDownloadTask) {
        try {
            ॱ(sessionDownloadTask);
        } catch (IllegalStateException e) {
            Logger.ˋ("HiAppDownload", "startDownloadByNormal exception:" + e.getMessage());
            ˏ(sessionDownloadTask);
        }
    }

    private static void ॱ(SessionDownloadTask sessionDownloadTask) {
        Logger.ˎ("HiAppDownload", "DownloadServiceHelper startDownloadByNormal");
        Intent intent = new Intent(bnl.ˏ().ॱ(), DownloadService.class);
        intent.putExtra("sessionId", sessionDownloadTask.ʾ());
        bnl.ˏ().ॱ().startService(intent);
    }

    private static void ˏ(SessionDownloadTask sessionDownloadTask) {
        Logger.ˎ("HiAppDownload", "DownloadServiceHelper startDownloadByBind");
        Intent intent = new Intent(bnl.ˏ().ॱ(), DownloadService.class);
        intent.putExtra(HiAnalyticsConstant.BI_KEY_PACKAGE, sessionDownloadTask.ˊˊ());
        intent.putExtra("sessionId", sessionDownloadTask.ʾ());
        if (ˊ == null) {
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            linkedHashMap.put("errorMsg", "");
            linkedHashMap.put("errorCode", String.valueOf(0));
            linkedHashMap.put("option", String.valueOf(1));
            cdl.ˎ("038", linkedHashMap);
            synchronized (ˏ) {
                ॱ.add(Long.valueOf(sessionDownloadTask.ʾ()));
            }
            synchronized (ʽ) {
                if (ˋ == null) {
                    ˋ = new b();
                }
                bnl.ˏ().ॱ().bindService(intent, ˋ, 1);
            }
            return;
        }
        LinkedHashMap linkedHashMap2 = new LinkedHashMap();
        linkedHashMap2.put("errorMsg", "");
        linkedHashMap2.put("errorCode", String.valueOf(0));
        linkedHashMap2.put("option", String.valueOf(3));
        cdl.ˎ("038", linkedHashMap2);
        ˊ.doDownload(sessionDownloadTask.ʾ());
    }

    public static void ˎ() {
        synchronized (ʽ) {
            if (ˋ != null) {
                Logger.ˎ("HiAppDownload", "DownloadServiceHelper unbind");
                try {
                    bnl.ˏ().ॱ().unbindService(ˋ);
                } catch (Exception e) {
                    Logger.e("HiAppDownload", "failed to unbind service");
                }
                ˊ = null;
                ˋ = null;
            }
        }
    }
}
